package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.domain.Account;
import com.ruoyi.basedata.service.IAccountService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 销户销记录Controller
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/basedata/account")
public class AccountController extends BaseController {
    @Autowired
    private IAccountService accountService;

    /**
     * 查询销户销记录列表
     */
    @PreAuthorize("@ss.hasAnyPermi('basedata:account:list,logs:exchange:list')")
    @GetMapping("/list")
    public TableDataInfo list(Account account) {
        startPage();
        if (account.getTerminalType() == 1) {
            List<Account> accounts = accountService.selectAccountListDb(account);
            return getDataTable(accounts);
        }
        List<Account> list = accountService.selectAccountListXb(account);
        return getDataTable(list);
    }

    /**
     * 导出销户销记录列表
     */
    @PreAuthorize("@ss.hasAnyPermi('basedata:account:export,logs:exchange:list')")
    @Log(title = "销户销记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Account account) {
        if (account.getTerminalType() == 1) {
            List<Account> accounts = accountService.selectAccountListDb(account);
            ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
            util.exportExcel(response, accounts, "销户销记录数据");
        }
        List<Account> list = accountService.selectAccountListXb(account);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        util.exportExcel(response, list, "销户销记录数据");
    }

    /**
     * 获取销户销记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accountService.selectAccountById(id));
    }

    /**
     * 新增销户销记录
     */
    @PreAuthorize("@ss.hasPermi('basedata:account:add')")
    @Log(title = "销户销记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Account account) {
        return toAjax(accountService.insertAccount(account));
    }

    /**
     * 修改销户销记录
     */
    @PreAuthorize("@ss.hasPermi('basedata:account:edit')")
    @Log(title = "销户销记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Account account) {
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除销户销记录
     */
    @PreAuthorize("@ss.hasPermi('basedata:account:remove')")
    @Log(title = "销户销记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(accountService.deleteAccountByIds(IDs));
    }
}
